package handler

import (
	"ArticleServer/api/internal/config"
	"ArticleServer/api/internal/middleware"
	"ArticleServer/api/internal/svc"
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
	"net/http"
)

var SC *svc.ServiceContext

func InitRouter(SysCfg config.System) *gin.Engine {
	// 设置gin的模式
	gin.SetMode(SysCfg.Env)
	// 获得一个默认的gin引擎
	Router := gin.Default()
	// 加载跨域中间件
	Router.Use(middleware.Cors())

	// 分成两大块，一个是需要认证一个是不需要认证
	UAuth := Router.Group("/artUAuth")
	{
		UAuth.GET("article", func(c *gin.Context) {
			c.JSON(http.StatusOK, gin.H{"status": "ok"})
		})
	}

	Auth := Router.Group("/artAuth")
	{
		Auth.GET("article", func(c *gin.Context) {
			//claims := c.GetHeader("claims")
			//fmt.Println("claims:", claims)
			c.JSON(http.StatusOK, gin.H{"status": "ok"})
		})
	}

	var store = cookie.NewStore([]byte(SysCfg.SessionsSecret))
	Router.Use(sessions.Sessions("session", store))

	return Router
}
